<?php
/**
*
* @package ppkBB3cker
* @version $Id: viewtopic_add1.php 1.000 2008-11-14 17:20:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$dt=time();

$u_status=array();
$ajax_url="{$phpbb_root_path}tracker/ajax.{$phpEx}?p={$row['post_id']}";
$is_canusefree = ($auth->acl_get('u_canusefree') && $auth->acl_get('f_canusefree', $forum_id)) && $user->data['is_registered'] ? 1 : 0;

$is_canuseup = ($auth->acl_get('u_canuseup') && $auth->acl_get('f_canuseup', $forum_id)) && $user->data['is_registered'] ? 1 : 0;
$is_canusebonus = ($auth->acl_get('u_canusebonus') && $auth->acl_get('f_canusebonus', $forum_id)) && $user->data['is_registered'] ? 1 : 0;

$is_canuseseedreq = ($auth->acl_get('u_canuseseedreq') && $auth->acl_get('f_canuseseedreq', $forum_id) && $user->data['user_id']!=$poster_id) && $user->data['is_registered'] ? 1 : 0;
$is_canskiprcheck = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskiprcheck') && $auth->acl_get('f_canskiprcheck', $forum_id)) ? 1 : 0) : 1;
$is_canskiprequpload = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskiprequpload') && $auth->acl_get('f_canskiprequpload', $forum_id)) ? 1 : 0) : 1;
$is_canskipreqratio = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskipreqratio') && $auth->acl_get('f_canskipreqratio', $forum_id)) ? 1 : 0) : 1;
$is_cansetforb = ($auth->acl_get('u_cansetforb') && $auth->acl_get('f_cansetforb', $forum_id)) ? 1 : 0;

$is_cansetstatus = ($auth->acl_get('u_cansetstatus') && $auth->acl_get('f_cansetstatus', $forum_id)) ? 1 : 0;
$is_cansetsticky = $auth->acl_get('f_sticky', $forum_id) ? 1 : 0;
$is_admod=$auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_') ? 1 : 0;
$ppkbb_tcrewr_updown=my_split_config($config['ppkbb_tcrewr_updown'], 3, 'my_int_val');
$user->add_lang('mods/ppkbb3cker_statuses');
if(!isset($user->lang['TRACKER_FORB_MARK'][$torrents[$torrent_id]['forb']]))
{
	$u_status[$torrents[$torrent_id]['forb']]=$torrents[$torrent_id]['forb'];
	$torrents[$torrent_id]['forb']=0;
}
$torrent_link=1;
$ppkbb_tcauthor_candown=my_split_config($config['ppkbb_tcauthor_candown'], 0 , 'intval');
$torrents[$torrent_id]['forb'] < 1 || ($user->data['user_id']==$poster_id && in_array($torrents[$torrent_id]['forb'], $ppkbb_tcauthor_candown)) ? '' : $torrent_link=0;
!$user->data['is_registered'] && !$ppkbb_tcguests_enabled[0] ? $torrent_guest=0 : $torrent_guest=1;

$user_ratio=get_ratio($user->data['user_uploaded'], $user->data['user_downloaded'], $config['ppkbb_tcratio_start'], $user->data['user_bonus']);


$t_elapsed = intval(($dt - @$torrents[$torrent_id]['added']) / 3600);

$t_wait=-1;
if(!$ppkbb_tcrewr_updown[2] && !$is_canskiprcheck && $config['ppkbb_tcwait_time'])
{
	$t_wait=get_trestricts($user->data['user_uploaded'], $user->data['user_downloaded'], $user_ratio, $config['ppkbb_tcwait_time'], 'up');

	if ($t_wait > 0)
	{
		$t_elapsed < $t_wait ? $t_wait=$t_wait - $t_elapsed : $t_wait=-1;
	}
	$t_wait >= 0 ? $torrent_link=0 : '';
}

$t_wait2=-1;
if(!$ppkbb_tcrewr_updown[2] && !$is_canskiprcheck && $config['ppkbb_tcwait_time2'])
{
	$t_wait2=get_trestricts($user->data['user_uploaded'], $user->data['user_downloaded'], $user_ratio, $config['ppkbb_tcwait_time2']);
	if ($t_wait2 > 0)
	{
		$t_elapsed < $t_wait2 ? $t_wait2=$t_wait2 - $t_elapsed : $t_wait2=-1;
	}
	$t_wait2 >= 0 ? $torrent_link=0 : '';
}

$torrent_info_curr='';

$torrent_info_curr_explain=$assign_vars=$torrent_info=$torrent_stat=$torrent_action=$sort_opt=$postrow_headers=$postrow_header=array();

$torrent_opt=request_var('opt', '');
/*$opt_sort=request_var('opts', '');
$opt_param=request_var('optp', '');
$opt_sort=='ASC' ? '' : $opt_sort='DESC';*/

!$is_candowntorr || !$torrent_guest/* || !$torrent_link*/ ? $torrent_opt='' : '';
if($torrent_guest && $is_candowntorr && $torrent_link)
{
	$ppkbb_torrent_statvt=my_split_config($config['ppkbb_torrent_statvt'], 9, 'my_int_val');
	$torrents_info=array('filelist', 'finished', 'seed', 'leech', 'thanks', 'history', 'leave', 'seedreq', 'remote');
	foreach($torrents_info as $ik=>$iv)
	{
		if(($iv=='seedreq' && !$config['ppkbb_srenable']) || ($iv=='remote' && !$ppkbb_tcenable_rannounces[0]) || (!$ppkbb_torrent_statvt[$ik]))
		{
			$torrent_opt='';
			continue;
		}
		$ajax_viewtopic_url=str_replace('&amp;', '&', append_sid("{$phpbb_root_path}tracker/ajax.{$phpEx}?p={$row['post_id']}&amp;opt={$iv}&amp;page=vt"));
		$torrent_info[$iv]='<a class="torrent_stat" rel="'.$ajax_viewtopic_url.'" href="javascript:;">'.$user->lang['TORRENT_INFO_HEADER_'.strtoupper($iv)].'</a>';
		//'.$viewtopic_url.($torrent_opt!=$iv ? '&amp;opt='.$iv.'#opt' : '').'
	}
}

if($is_cansetforb || $is_cansetstatus)
{
	/*$set_forb=request_var('set_forb', '');
	$forb_val=request_var('forb_val', 0);
	!isset($user->lang['TRACKER_FORB_MARK'][$forb_val]) || $forb_val > 99 || $forb_val < -99 ? $forb_val=0 : '';
	!$is_cansetforb && ($forb_val > -50 || $torrents[$torrent_id]['forb'] > 0) ? $set_forb=0 : '';
	!$is_cansetstatus && $forb_val < -49 ? $set_forb=0 : '';
	$forb_locked=request_var('forb_locked', 0) ? 1 : 0;
	$forb_closed=request_var('forb_closed', 0) ? 1 : 0;
	$forb_val==1 ? $forb_locked=1 : '';
	$forb_val==0 ? $forb_locked=0 : '';
	if($set_forb)
	{
		$forb_reason =!empty($_POST['forb_reason']) ? utf8_normalize_nfc(request_var('forb_reason', '', true)) : '';
		$result=$db->sql_query('UPDATE '. POSTS_TABLE ." SET post_edit_locked='$forb_locked' WHERE post_id='{$row['post_id']}'");
		$result=$db->sql_query('UPDATE '. TOPICS_TABLE ." SET topic_status='$forb_closed' WHERE topic_id='{$row['topic_id']}'");
		$result=$db->sql_query('UPDATE '. TRACKER_TORRENTS_TABLE ." SET forb='$forb_val', forb_reason='".$db->sql_escape(truncate_string($forb_reason, 255, 255, false))."', forb_date='$dt', forb_user_id='{$user->data['user_id']}' WHERE post_msg_id='{$row['post_id']}'");

		$ppkbb_tstatus_notify=my_split_config($config['ppkbb_tstatus_notify'], 2, 'my_int_val');
		if($user->data['user_id']!=$poster_id && (($ppkbb_tstatus_notify[0] && $forb_val < 1 && $torrents[$torrent_id]['forb'] > 0) || ($ppkbb_tstatus_notify[1] && $forb_val  > 0 && $torrents[$torrent_id]['forb'] < 1)))
		{
			$approve=array(
				'from_user_id'=>$user->data['user_id'],
				'icon_id'=>0,
				'from_user_ip'=>$_SERVER['REMOTE_ADDR'],
				'from_username'=>$user->data['username'],
				'enable_bbcode'=>0,
				'enable_smilies'=>0,
				'enable_urls'=>0,
				'enable_sig'=>0,
				'message'=>sprintf($user->lang['TORRENT_NOTIFY_TEXT'], append_sid($phpbb_root_path."viewtopic.{$phpEx}?f={$forum_id}&amp;t={$topic_id}"), $topic_data['topic_title'], $user->lang['TRACKER_FORB_REASON'][$forb_val], $user->lang['TRACKER_FORB_REASON'][$torrents[$torrent_id]['forb']], get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), htmlspecialchars($forb_reason), $user->format_date($dt, 'Y-m-d H:i:s')),
				'bbcode_bitfield'=>0,
				'bbcode_uid'=>0,
				'to_address'=>'',
				'bcc_address'=>'',
			);
			$approve['address_list']['u'][$poster_id]='to';
			if(isset($approve['address_list']['u']))
			{
				include_once($phpbb_root_path.'includes/functions_privmsgs.'.$phpEx);
				submit_pm('post', $user->lang['TORRENT_NOTIFY_SUBJECT'], $approve, false);
			}
		}
		$row['post_edit_locked']=$forb_locked;
		$topic_data['topic_status']=$forb_closed;
		$torrents[$torrent_id]['forb_reason']=$forb_reason;
		$torrents[$torrent_id]['forb_user_id']=$user->data['user_id'];
		$torrents[$torrent_id]['username']=$user->data['username'];
		$torrents[$torrent_id]['user_colour']=$user->data['user_colour'];
		$torrents[$torrent_id]['forb_date']=$dt;
		$torrents[$torrent_id]['forb']=$forb_val;
		$ppkbb_tcauthor_candown=my_split_config($config['ppkbb_tcauthor_candown'], 0 , 'intval');
		$torrent_link=$forb_val > 0 ? ($user->data['user_id']==$poster_id && in_array($forb_val, $ppkbb_tcauthor_candown) ? 1 : 0) : 1;
		trigger_error($user->lang['TORR_FORB_CHANGED'].'<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' .$viewtopic_url. '">', '</a>'));
	}*/
	//if($is_cansetforb || $is_cansetstatus)
	//{
		$form_forb='<form action="javascript:void(0);" onsubmit="return false;" method="post" id="hj_set_forb"><input type="hidden" name="set_forb" id="set_forb" value="1" /><input type="submit" name="submit_set_forb" class="button1" value="'.$user->lang['TORRENT_ACTION'].'" /> '.$user->lang['TORRENT_INFO_HEADER_SETFORBIDDEN'].'<br />'.$user->lang['TORRENT_STATUS'].': <select name="forb_val" id="forb_val">';
		ksort($user->lang['TRACKER_FORB_REASON']);
		$forb_sel=array();
		foreach($user->lang['TRACKER_FORB_REASON'] as $rk => $rv)
		{
			/*if(!$is_cansetforb && $rk > -50)
			{
				continue;
			}
			if(!$is_cansetstatus && $rk < -49)
			{
				continue;
			}*/
			if($rk < -49 && @!$forb_sel[-50])
			{
				$form_forb.='<option disabled="disabled">'.$user->lang['TRACKER_FORB_SREASON'].'</option>';
				$forb_sel[-50]=1;
			}
			if($rk < 0 && $rk > -50 && @!$forb_sel[-1])
			{
				$form_forb.='<option disabled="disabled">'.$user->lang['TRACKER_FORB_MREASON'].'</option>';
				$forb_sel[-1]=1;
			}
			if($rk > 0 && @!$forb_sel[1])
			{
				$form_forb.='<option disabled="disabled">'.$user->lang['TRACKER_FORB_PREASON'].'</option>';
				$forb_sel[1]=1;
			}
			if($rk == 0 && @!$forb_sel[0])
			{
				$form_forb.='<option disabled="disabled">'.$user->lang['TRACKER_FORB_UREASON'].'</option>';
				$forb_sel[0]=1;
			}
			$form_forb.='<option value="'.$rk.'"'.($torrents[$torrent_id]['forb']==$rk ? ' selected="selected"' : '').((!$is_cansetforb && ($rk > -50 || $torrents[$torrent_id]['forb'] > 0)) || (!$is_cansetstatus && $rk < -49) ? ' disabled="disabled"' : '').'>'.$rv.'</option>';
		}
		//$form_forb.='</select><br /><input type="text" maxlength="255" name="forb_reason" id="edit_reason" value="'.$torrents[$torrent_id]['forb_reason'].'" class="inputbox" title="'.$user->lang['BLOCKED_REASON'].'" />&nbsp;<input type="checkbox" name="forb_locked" value="1" title="'.$user->lang['BLOCKED_LOCKED'].'"'.($row['post_edit_locked'] ? ' checked="checked"' : '').' /><input type="checkbox" name="forb_closed" value="1" title="'.$user->lang['BLOCKED_CLOSED'].'"'.($topic_data['topic_status'] ? ' checked="checked"' : '').' /></form>';
		$form_forb.='</select><br />'.$user->lang['BLOCKED_REASON'].':<br /><input type="text" maxlength="255" name="forb_reason" id="forb_reason" value="'.$torrents[$torrent_id]['forb_reason'].'" class="inputbox autowidth" size="28" /><br /> <input type="checkbox" name="forb_locked" id="forb_locked" value="1"'.($row['post_edit_locked'] ? ' checked="checked"' : '').' /> - '.$user->lang['BLOCKED_LOCKED'].'<br /> <input type="checkbox" name="forb_closed" id="forb_closed" value="1"'.($topic_data['topic_status'] ? ' checked="checked"' : '').' /> - '.$user->lang['BLOCKED_CLOSED'].'</form>';
		$torrent_action['TA_FORB_FORM']=$form_forb;
	//}
}

if(sizeof($torrents_hashes))
{
	$r_torr=$r_exs=$torrents_remote=array();
	$torrents_id=array_keys($torrents_hashes);
	$sql='SELECT rt.id, rt.torrent torrent2, ra.torrent, ra.next_announce FROM '.TRACKER_RTRACK_TABLE.' rt LEFT JOIN '.TRACKER_RANNOUNCES_TABLE." ra ON (rt.id=ra.tracker AND ra.torrent IN('".(implode("', '", $torrents_id))."')) WHERE (rt.zone_id='0' AND rt.rtrack_remote='1' AND rt.torrent='0') OR rt.torrent IN('".(implode("', '", $torrents_id))."')";
	$result=$db->sql_query($sql);

	while($row_remote=$db->sql_fetchrow($result))
	{
		if(!$row_remote['torrent2'])
		{
			$r_torr['all'][$row_remote['id']]=$torrents_hashes;
		}
		else
			{
			isset($torrents_hashes[$row_remote['torrent2']]) ? $r_torr['torr'][$row_remote['id']][$row_remote['torrent2']]=$torrents_hashes[$row_remote['torrent2']] : '';
			}
		if($row_remote['torrent'])
			{
			$r_exs[$row_remote['torrent2'].'_'.$row_remote['torrent']][$row_remote['id']]=$row_remote;
			}
	}
	$db->sql_freeresult($result);

	if(isset($r_torr['all']))
			{
		foreach($r_torr['all'] as $tr_id => $a_data)
		{
			foreach($a_data as $t_id => $t_hash)
			{
				if(isset($r_exs['0_'.$t_id][$tr_id]))
				{
					if($dt > $r_exs['0_'.$t_id][$tr_id]['next_announce'])
					{
						$torrents_remote[$t_id]=$torrents_hashes[$t_id];
					}
			}
			else
			{
					$torrents_remote[$t_id]=$torrents_hashes[$t_id];

			}
		}
		}
		unset($r_torr['all']);
	}
	if(isset($r_torr['torr']))
		{
		foreach($r_torr['torr'] as $tr_id => $a_data)
			{
			foreach($a_data as $t_id => $t_hash)
			{
				if(isset($r_exs[$t_id.'_'.$t_id][$tr_id]))
				{
					if($dt > $r_exs[$t_id.'_'.$t_id][$tr_id]['next_announce'])
					{
						$torrents_remote[$t_id]=$torrents_hashes[$t_id];
			}
		}
				else
	{
					$torrents_remote[$t_id]=$torrents_hashes[$t_id];
}

}
		}
		unset($r_torr['torr']);
	}
	unset($torrents_hashes);
}

$seed_percent=0;
if($torrents[$torrent_id]['seeders']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_seeders'] : 0) < $torrents[$torrent_id]['times_completed']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_times_completed'] : 0)+1)
{
	$seed_percent=my_int_val(($torrents[$torrent_id]['seeders']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_seeders'] : 0)) * 100 / ($torrents[$torrent_id]['times_completed']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_times_completed'] : 0)+1));
}
else
{
	$seed_percent=100;
}

$sr_addon=array();
$ppkbb_srincl_ratio=my_split_config($config['ppkbb_srincl_ratio']);
if($config['ppkbb_srenable'] && $is_canuseseedreq && $torrents[$torrent_id]['forb'] < 1)
{
	if($torrents[$torrent_id]['size'] > $config['ppkbb_srfile_size']*1024*1024 && ((sizeof($ppkbb_srincl_ratio) && in_array($user_ratio, $ppkbb_srincl_ratio)) || $user_ratio > $config['ppkbb_srreq_ratio']) && ($seed_percent <= $config['ppkbb_srseed_percent'] || !$config['ppkbb_srseed_percent']) && $torrent_link)
	{
		$d_seedreq=0;
		$ppkbb_srreq_time=intval($config['ppkbb_srreq_time'] * 60 * 60);
		$ppkbb_srreq_time ? '' : $ppkbb_srreq_time=600;
		if($ppkbb_srreq_time)
		{
			/*$sql="SELECT added FROM ".TRACKER_SEEDREQUEST_TABLE." WHERE (user_id='{$user->data['user_id']}' OR torrent='{$torrent_id}') AND $dt - added < {$ppkbb_srreq_time} LIMIT 1";
			$result=$db->sql_query($sql);
			$d_seedreq=$db->sql_fetchrow($result);
			$db->sql_freeresult($result);*/
			if($torrents[$torrent_id]['lastseedreq'] > $user->data['user_last_seedreq'] || $user->data['user_last_seedreq'] == $torrents[$torrent_id]['lastseedreq'])
			{
				$d_seedreq=$torrents[$torrent_id]['lastseedreq'];
			}
			else
			{
				$d_seedreq=$user->data['user_last_seedreq'];
			}
			if($dt - $d_seedreq < $ppkbb_srreq_time)
			{
				//$d_seedreq=1;
			}
			else
			{
				$d_seedreq=0;
			}
		}
		if(!$d_seedreq)
		{
			/*if(request_var('seed_request', ''))
			{
				$ppkbb_sruser_lvisit=intval($config['ppkbb_sruser_lvisit'] * 24 * 60 * 60);
				$ppkbb_srexcl_ratio=floatval($config['ppkbb_srexcl_ratio']);
				$sql="SELECT u.username, u.user_id FROM ". TRACKER_SNATCHED_TABLE .' s LEFT JOIN '.TRACKER_PEERS_TABLE.' p ON p.userid=s.userid AND p.torrent=s.torrent LEFT JOIN '. USERS_TABLE ." u ON s.userid=u.user_id LEFT JOIN ".SESSIONS_TABLE." st ON u.user_id=st.session_user_id WHERE s.torrent='$torrent_id'".($ppkbb_sruser_lvisit ? " AND IF(st.session_user_id, '$dt' - st.session_time < '{$ppkbb_sruser_lvisit}', '$dt' - u.user_lastvisit < '{$ppkbb_sruser_lvisit}')" : '').($ppkbb_srexcl_ratio ? " AND (s.uploaded='0' OR s.uploaded/s.downloaded < '{$ppkbb_srexcl_ratio}')" : '')." AND ISNULL(p.torrent) AND s.finished".($config['ppkbb_srusers_limit'] ? " ORDER BY RAND() LIMIT {$config['ppkbb_srusers_limit']}" : '');
				$result=$db->sql_query($sql);
				$leaves=array(
					'from_user_id'=>$user->data['user_id'],
					'icon_id'=>0,
					'from_user_ip'=>$_SERVER['REMOTE_ADDR'],
					'from_username'=>$user->data['username'],
					'enable_bbcode'=>0,
					'enable_smilies'=>0,
					'enable_urls'=>0,
					'enable_sig'=>0,
					'message'=>sprintf($user->lang['REQUEST_SEEDS'], append_sid($phpbb_root_path."viewtopic.{$phpEx}?f={$forum_id}&amp;t=$topic_id"), $topic_data['topic_title']),
					'bbcode_bitfield'=>0,
					'bbcode_uid'=>0,
					'to_address'=>'',
					'bcc_address'=>'',
					);
				$leaves['address_list']['u'][$poster_id]='to';
				while($userlist=$db->sql_fetchrow($result))
				{
					$leaves['address_list']['u'][$userlist['user_id']]='to';
				}
				$db->sql_freeresult($result);
				if(@$leaves['address_list']['u'])
				{
					include_once($phpbb_root_path.'includes/functions_privmsgs.'.$phpEx);
					$db->sql_query("INSERT INTO ".TRACKER_SEEDREQUEST_TABLE." (user_id, torrent, added) VALUES('{$user->data['user_id']}', '{$torrent_id}', '{$dt}')");
					submit_pm('post', $user->lang['REQUEST_SEEDS_SUBJECT'], $leaves, false);
					$db->sql_query("UPDATE ".USERS_TABLE." SET user_fromseedreq_count=user_fromseedreq_count+1 WHERE user_id='{$user->data['user_id']}'");
					$db->sql_query("UPDATE ".USERS_TABLE." SET user_toseedreq_count=user_toseedreq_count+1 WHERE user_id IN('".implode("', '", array_keys($leaves['address_list']['u']))."')");
					trigger_error($user->lang['TORR_SEEDREQ_SEND'].'<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' .$viewtopic_url. '">', '</a>'));
				}
			}
			else
			{*/
				$torrent_action['TA_SEEDREQ_FORM']='<form action="javascript:void(0);" onsubmit="return false;" method="post" id="hj_seed_request"><input type="hidden" name="seed_request" id="seed_request" value="1" /><input src="'.$user->img('icon_seed_request', '', '', '', 'src').'" style="border:0px;background-color:transparent;" type="image" name="submit_seed_request" title="'.$user->lang['TORRENT_INFO_HEADER_GETSEED'].'" /></form>';
			//}
		}
		else
		{
			$sr_addon=array($user->lang['REQUEST_SEEDS_NEXT'], get_time_diff($dt, $d_seedreq+$ppkbb_srreq_time));
		}
	}
}
$torrent_stat['TS_AUTHOR']=array($user->lang['TORRENT_AUTOR'], ($poster_id != ANONYMOUS) ? $user_cache[$poster_id]['author_full'] : get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']));
$torrent_stat['TS_ADDED']=array($user->lang['TORRENT_ADDED'], $user->format_date($torrents[$torrent_id]['added']));
$torrents[$torrent_id]['forb']!=0 && isset($user->lang['TRACKER_FORB_MARK'][$torrents[$torrent_id]['forb']]) ? $torrent_stat['TS_TSTATUS']=array($user->lang['TORRENT_STATUS'], $user->lang['TRACKER_FORB_MARK'][$torrents[$torrent_id]['forb']]) : '';
$torrents[$torrent_id]['forb_user_id'] ? $torrent_stat['TS_USTATUS']=array($user->lang['TORRENT_STATUS_USER'], get_username_string('full', $torrents[$torrent_id]['forb_user_id'], $torrents[$torrent_id]['username'], $torrents[$torrent_id]['user_colour']).' ('.$user->format_date($torrents[$torrent_id]['forb_date']).')') : '';
$torrents[$torrent_id]['forb_reason'] ? $torrent_stat['TS_REASON']=array($user->lang['TORRENT_STATUS_REASON'], $torrents[$torrent_id]['forb_reason']) : '';
$torrent_stat['TS_SEEDERS']=array($user->lang['TORRENT_SEEDERS'], $torrents[$torrent_id]['seeders']);
$torrent_stat['TS_LEECHERS']=array($user->lang['TORRENT_LEECHERS'], $torrents[$torrent_id]['leechers']);
$torrent_stat['TS_COMPLETED']=array($user->lang['TORRENT_COMPLETED'], $torrents[$torrent_id]['times_completed']);
$torrent_stat['TS_HEALTH']=array($user->lang['TORRENT_HEALTH'], get_torrent_health($torrents[$torrent_id]['seeders'], $torrents[$torrent_id]['leechers']).'%');
if($torrents[$torrent_id]['times_completed'])
{
	$torrent_stat['TS_SPERCENT']=array($user->lang['TORRENT_PERCENT'], ($config['ppkbb_srenable']==1 ? '<a href="javascript:;" title="'.$user->lang['WO_REM_PEERS'].'" class="my_tt">'.$seed_percent.'</a>' : $seed_percent).'%');
}
$torrent_stat['TS_SIZE']=array($user->lang['TORRENT_SIZE'], get_formatted_filesize($torrents[$torrent_id]['size']));
$torrent_stat['TS_SIZE2']=array($user->lang['TORRENT_SIZE'], number_format($torrents[$torrent_id]['size'], 0, '.', ' ').' '.$user->lang['BYTES']);

$dt - $torrents[$torrent_id]['added'] < $config['ppkbb_tstat_ctime'] * 60 && $config['ppkbb_tstat_ctime'] > 1 ? $ppkbb_tstat_ctime = 1 : $ppkbb_tstat_ctime=$config['ppkbb_tstat_ctime'];

$tsl_speed=my_split_config($torrents[$torrent_id]['tsl_speed'], 3, 'my_int_val');
$total_updown_speed=array();
if($torrents[$torrent_id]['forb'] < 1)
{
	if($dt - $tsl_speed[2] > $ppkbb_tstat_ctime*60)
	{
		$sql="SELECT SUM(s.uploadoffset/(s.last_action-s.prev_action)) up_speed, SUM(s.downloadoffset/(s.last_action-s.prev_action)) down_speed FROM ".TRACKER_SNATCHED_TABLE." s WHERE torrent='{$torrent_id}' AND {$dt}-s.last_action < {$config['ppkbb_tcdead_time']}";
		$result=$db->sql_query($sql);//, $ppkbb_tstat_ctime * 60
		$total_updown_speed=$db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		$sql="UPDATE ".TRACKER_TORRENTS_TABLE." SET tsl_speed='".intval($total_updown_speed['up_speed'])." ".intval($total_updown_speed['down_speed'])." {$dt}' WHERE id='{$torrent_id}'";
		$result=$db->sql_query($sql);
	}
	else
	{
		$total_updown_speed['up_speed']=$tsl_speed[0];
		$total_updown_speed['down_speed']=$tsl_speed[1];
	}
}
else
{
	$total_updown_speed['up_speed']=$total_updown_speed['down_speed']=0;
}
$torrent_stat['TS_LSPEED']=array($user->lang['TORRENT_LSPEED'], get_formatted_filesize(my_int_val($total_updown_speed['up_speed']), 1, false, 1));
$torrent_stat['TS_SSPEED']=array($user->lang['TORRENT_SSPEED'], get_formatted_filesize(my_int_val($total_updown_speed['down_speed']), 1, false,  1));

$last_seedleech=array();
$last_seedleech['last_seed']=$torrents[$torrent_id]['lastseed'];
$last_seedleech['last_leech']=$torrents[$torrent_id]['lastleech'];

$torrents[$torrent_id]['rem_seeders'] && $torrents[$torrent_id]['lastremote'] > $last_seedleech['last_seed'] ? $last_seedleech['last_seed']=$torrents[$torrent_id]['lastremote'] : '';
$torrent_stat['TS_LSEED']=array($user->lang['TORRENT_LASTSEED'], ($last_seedleech['last_seed'] ? sprintf($user->lang['TORRENT_LAST_SEEDLEECH'], get_time_diff($last_seedleech['last_seed'], $dt)) : $user->lang['NO']));
$torrents[$torrent_id]['rem_leechers'] && $torrents[$torrent_id]['lastremote'] > $last_seedleech['last_leech'] ? $last_seedleech['last_leech']=$torrents[$torrent_id]['lastremote'] : '';
$torrent_stat['TS_LLEECH']=array($user->lang['TORRENT_LASTLEECH'], ($last_seedleech['last_leech'] ? sprintf($user->lang['TORRENT_LAST_SEEDLEECH'], get_time_diff($last_seedleech['last_leech'], $dt)) : $user->lang['NO']));
$sr_addon ? $torrent_stat['TS_SEEDREQ']=$sr_addon : '';

$torrent_stat['TS_PRIVATE']=array($user->lang['TORRENT_PRIVATE'], $torrents[$torrent_id]['private'] ? $user->lang['TORRENT_PRIVATE_YES'] : $user->lang['TORRENT_PRIVATE_NO']);

$torrents[$torrent_id]['info_hash']=bin2hex($torrents[$torrent_id]['info_hash']);
$info_hash=wordwrap($torrents[$torrent_id]['info_hash'], 20, '<br />', true);
$torrent_stat['TS_HASH']=array($user->lang['TORRENT_HASH'], $info_hash);
//$torrent_stat['TS_HASH2']=array($user->lang['TORRENT_HASH'], $torrents[$torrent_id]['info_hash']);

//$ppkbb_portal_torrents_posttime=my_split_config($config['ppkbb_portal_torrents_posttime'], 3, array('intval', 'my_int_val', 'my_int_val'));
if(/*$ppkbb_portal_torrents_posttime[0] >= 0 && */$is_cansetsticky && $torrents[$torrent_id]['forb'] < 1)
{
	/*$set_sticky=request_var('set_sticky', 0);
	if($set_sticky)
	{
		$as_sticky=request_var('as_sticky', 0);
		$tlimit_sticky=request_var('torrent_time_limit', 0);
		$tlimit_sticky > 999 || $tlimit_sticky < 0 ? $tlimit_sticky=0 : $tlimit_sticky=$tlimit_sticky * 86400;
		if($as_sticky && $torrent_link)
		{
			$topic_data['topic_type']=POST_STICKY;
		}
		else
		{
			$topic_data['topic_type']=POST_NORMAL;
			$tlimit_sticky=0;
		}
		$topic_data['topic_time_limit']=$tlimit_sticky;
		$result=$db->sql_query('UPDATE '. TOPICS_TABLE ." SET topic_time_limit='".$tlimit_sticky."', topic_type='".$topic_data['topic_type']."' WHERE topic_id='{$topic_data['topic_id']}'");
		trigger_error($user->lang['TORR_STICKY_SET'].'<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' .$viewtopic_url. '">', '</a>'));
	}*/
	$left_sticky=0;
	if($topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) > $dt)
	{
		$left_sticky=get_time_diff($dt, $topic_data['topic_time'] + $topic_data['topic_time_limit']);
	}
	//$torrent_action['TA_STICKY_FORM']='<form action="'. $viewtopic_url .'" method="post">'.$user->lang['TORRENT_INFO_HEADER_SETSTICKY'].'<br /><input type="submit" name="set_sticky" class="button1" value="'.$user->lang['TORRENT_ACTION'].'" /> <input type="text" name="torrent_time_limit" id="topic_time_limit" size="3" maxlength="3" value="'.($topic_data['topic_time_limit'] / 86400).'" class="inputbox autowidth" title="'.$user->lang['STICKY_DESCR'].'" />&nbsp;<input type="checkbox" name="as_sticky" value="1" title="'.$user->lang['TORRENT_INFO_HEADER_STICKY'].'"'.($topic_data['topic_type']==POST_STICKY ? ' checked="checked"' : '').' />'.($left_sticky ? '&nbsp;'.$left_sticky : '').'</form>';
	$torrent_action['TA_STICKY_FORM']='<form action="javascript:void(0);" onsubmit="return false;" method="post" id="hj_set_sticky"><input type="hidden" name="set_sticky" id="set_sticky" value="1" /><input type="submit" name="submit_set_sticky" class="button1" value="'.$user->lang['TORRENT_ACTION'].'" /> '.$user->lang['TORRENT_INFO_HEADER_SETSTICKY'].'<br />'.$user->lang['STICKY_DESCR'].':<br /><input type="text" name="torrent_time_limit" id="torrent_time_limit" size="3" maxlength="3" value="'.($topic_data['topic_time_limit'] / 86400).'" class="inputbox autowidth" /> <input type="checkbox" name="as_sticky" id="as_sticky" value="1"'.($topic_data['topic_type']==POST_STICKY ? ' checked="checked"' : '').' /> - '.$user->lang['TORRENT_INFO_HEADER_STICKY'].($left_sticky ? '<br /><div id="sticky_left">'.$user->lang['STICKY_EXPIRE'].': '.$left_sticky.'</div>' : '<div id="sticky_left"></div>').'</form>';
}

$dthanks=0;
if(!$torrents[$torrent_id]['thanks'] && $torrents[$torrent_id]['poster_id']!=$user->data['user_id'] && $is_canusethanks && $torrent_link)
{
	/*$say_thanks=request_var('say_thanks', 0);
	!$say_thanks ? $say_thanks=(request_var('submit_thanks_x', '') || request_var('submit_thanks_y', '')) : '';
	if($say_thanks)
	{
		$db->sql_query('INSERT INTO '. TRACKER_THANKS_TABLE ." VALUES (0, '{$user->data['user_id']}', '$torrent_id', '{$torrents[$torrent_id]['poster_id']}', '$dt')");
		$db->sql_query("UPDATE ".USERS_TABLE." SET user_tothanks_count=user_tothanks_count+1 WHERE user_id='{$torrents[$torrent_id]['poster_id']}'");
		$db->sql_query("UPDATE ".USERS_TABLE." SET user_fromthanks_count=user_fromthanks_count+1 WHERE user_id='{$user->data['user_id']}'");
		$dthanks=1;
		trigger_error($user->lang['TORR_THANKS_SEND'].'<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' .$viewtopic_url. '">', '</a>'));
	}*/
	!$dthanks ? $torrent_action['TA_THANKS_FORM']='<form action="javascript:void(0);" method="post" id="hj_thanks"><input type="hidden" name="say_thanks" id="say_thanks" value="1" /><input src="'.$user->img('icon_say_thanks', '', '', '', 'src').'" style="border:0px;background-color:transparent;" type="image" name="submit_thanks" title="'.$user->lang['TORRENT_INFO_HEADER_SAYTHANKS'].'" /></form>' : '';
	}
/*$i3=0;
$viewtopic_add1inc=$phpbb_root_path.'tracker/include/';

switch($torrent_opt)
{
	case 'leave':
		include($viewtopic_add1inc.'viewtopic_add1_leave.'.$phpEx);
		break;

	case 'history':
		include($viewtopic_add1inc.'viewtopic_add1_history.'.$phpEx);
		break;

	case 'filelist':
		include($viewtopic_add1inc.'viewtopic_add1_filelist.'.$phpEx);
		break;

	case 'finished':
		include($viewtopic_add1inc.'viewtopic_add1_finished.'.$phpEx);
		break;

	case 'seed':
		include($viewtopic_add1inc.'viewtopic_add1_seed.'.$phpEx);
		break;

	case 'leech':
		include($viewtopic_add1inc.'viewtopic_add1_leech.'.$phpEx);
		break;

	case 'thanks':
		include($viewtopic_add1inc.'viewtopic_add1_thanks.'.$phpEx);
		break;

	case 'seedreq':
		include($viewtopic_add1inc.'viewtopic_add1_seedreq.'.$phpEx);
		break;
	case 'remote':
		include($viewtopic_add1inc.'viewtopic_add1_remote.'.$phpEx);
		break;

	default:
		$torrent_opt='';
		break;
}*/

//$torrent_info_curr_explain[$torrent_opt]=$user->lang['TORRENT_INFO_HEADER_'.strtoupper($torrent_opt)];

$torrent_top_notice=$torrent_bottom_notice=false;
if($user->data['is_registered'])
{
	$ppkbb_noticedisclaimer_blocks[1] && $user->lang['TORRENT_TOP_NOTICE'] ? $torrent_top_notice=$user->lang['TORRENT_TOP_NOTICE'] : '';
	$ppkbb_noticedisclaimer_blocks[2] && $user->lang['TORRENT_BOTTOM_NOTICE'] ? $torrent_bottom_notice=$user->lang['TORRENT_BOTTOM_NOTICE'] : '';
}
else
{
	$ppkbb_noticedisclaimer_blocks[3] && $user->lang['TORRENT_TOP_NOTICE_GUEST'] ? $torrent_top_notice=$user->lang['TORRENT_TOP_NOTICE_GUEST'] : '';
	$ppkbb_noticedisclaimer_blocks[4] && $user->lang['TORRENT_BOTTOM_NOTICE_GUEST'] ? $torrent_bottom_notice=$user->lang['TORRENT_BOTTOM_NOTICE_GUEST'] : '';
}

$postrow=array_merge($postrow, array(
	'S_AJAX_THANKS' => str_replace('&amp;', '&', append_sid($ajax_url, 'opt=say_thanks')),
	'S_AJAX_SEED_REQUEST' => str_replace('&amp;', '&', append_sid($ajax_url, 'opt=seed_request')),
	'S_AJAX_SET_STICKY' => str_replace('&amp;', '&', append_sid($ajax_url, 'opt=set_sticky')),
	'S_AJAX_SET_FORB' => str_replace('&amp;', '&', append_sid($ajax_url, 'opt=set_forb')),
	'S_HAS_TRACKER_POSTER'	=> (@$torrents_attach['poster'] && $ppkbb_torr_blocks[2] ) ? true : false,
	'S_HAS_TRACKER_TORRENT'	=> (@$torrents_attach['torrent'] && $torrent_guest && $ppkbb_torr_blocks[1]) ? true : false,

	'S_HAS_TRACKER_SCREENSHOT'	=> (@$torrents_attach['screenshot'] && $ppkbb_torr_blocks[3]) ? true : false,
	'S_HAS_VOTE' => $tracker_poll && $ppkbb_torr_blocks[5] ? true : false,
	'TORRENT_INFO_STAT'=>$torrent_stat && $ppkbb_torr_blocks[0] ? true : false,
	'TORRENT_INFO_ACTION'=>$torrent_action && $ppkbb_torr_blocks[4] ? true : false,
	'TORRENT_INFO_OPT'=>$is_candowntorr ? implode(' : ', $torrent_info) : '',
	'TORRENT_INFO_AUTHOR' => $ppkbb_torr_blocks[8] ? true : false,

	'S_TORRENT_TOP_NOTICE' => $torrent_top_notice,
	'S_TORRENT_BOTTOM_NOTICE' => $torrent_bottom_notice,

/*
	'S_HAS_TORRENT_EXPLAIN'	=> $torrent_info_curr ? true : false,
	'S_HAS_TORRENT_EXPLAIN_FILELIST'	=> @$torrent_info_curr_explain['filelist'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_FINISHED'	=> @$torrent_info_curr_explain['finished'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_THANKS'	=> @$torrent_info_curr_explain['thanks'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_HISTORY'	=> @$torrent_info_curr_explain['history'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_LEAVE'	=> @$torrent_info_curr_explain['leave'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_SEED'	=> @$torrent_info_curr_explain['seed'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_LEECH'	=> @$torrent_info_curr_explain['leech'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_SEEDREQ'	=> @$torrent_info_curr_explain['seedreq'] ? true : false,
	'S_HAS_TORRENT_EXPLAIN_REMOTE'	=> $ppkbb_tcenable_rannounces[0] && @$torrent_info_curr_explain['remote'] ? true : false,
*/

	'MESS_FORUM_IMAGE'			=> $forum_image ? '<img src="' . $phpbb_root_path . $forum_image . '" alt="' . $topic_data['forum_name'] . '"  title="' . $topic_data['forum_name'] . '"/>' : '',
	'MESS_FORUM_SRC_IMAGE'			=> $forum_image ? $phpbb_root_path . $forum_image : '',
	'MESS_FORUM_NAME'			=> $topic_data['forum_name'],
));

//if($ppkbb_torr_blocks[0])
//{
	if(sizeof($torrent_stat))
	{
		$torrent_stats=array();
		foreach($torrent_stat as $sk => $sv)
		{
			$torrent_stats[$sk.'_KEY']=$sv[0];
			$torrent_stats[$sk.'_VALUE']=$sv[1];
		}
		$postrow=array_merge($postrow, $torrent_stats);
	}
	if(sizeof($torrent_action))
	{
		$postrow=array_merge($postrow, $torrent_action);
	}
//}
?>
